package com.chenchao.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * @encoding : UTF-8
 * @Time : 2020/12/5 14:46
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.reflection.Test12.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 反射越过泛型检查
 */
public class Test12 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        Class<? extends ArrayList> aClass = integers.getClass();
        Method add = aClass.getMethod("add", Object.class);
        Method add1 = aClass.getMethod("add", int.class, Object.class);
        add1.invoke(integers, 1,"Hello2");
        Object hello = add.invoke(integers, "Hello");
        /*相当于反射把参数类型开放成了Object*/
        System.out.println(integers);
    }
}
